Grokking Python Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Course Overview
Python Basics
Python Overview
Python Application Areas
Your First Python Program
Python - Basic Syntax
Python Comments
Quiz
Python Variables and Operators
Python - Variables
Python - Data types
Python - Numbers
Python - Booleans
Python - Type Conversions
Python - Operators
Quiz
Python Conditionals
Introduction to Conditional Statements in Python
Python - If...Else
Python - Match Case
Quiz
Python loops
Introduction to loops in Python
Python - for loop
Python - while loop
Python - Loop Control
Python - for else and while else loop
Quiz
Python Strings
Introduction to Strings in Python
Python - String Operators
Python Slicing Strings
Python - String Methods
Quiz
Python Array
Introduction to Array in Python
Python - Array Methods
Quiz
Python Lists
Introduction to Lists in Python
Python - Add and Remove List Items
Python - List Comprehension
Python - List Methods
Quiz
Python Tuples
Introduction to Tuple in Python
Python - Unpack Tuple Items
Python - Join Tuples
Python - Tuple Methods
Quiz
Python -Dictionaries
Introduction to Dictionary in Python
Python - Nested Dictionaries
Python - Dictionary Methods
Quiz
Python -Sets
Introduction to set in Python
Python - set Operations
Python - Set Methods
Quiz
Python Functions and Modules
Introductions to Function in Python
Python - Function Arguments
Recursive Functions
Python - Variable scope
Importing Modules
Quiz
Importing Modules

Modules in Python are simply files containing Python code that define functions, variables, and classes, which can be used once they are imported into another Python script.

Modules help break down complex programs into smaller, manageable, and reusable parts. They not only help in maintaining and organizing code efficiently but also in sharing functionalities between projects. Python comes with a rich standard library composed of numerous modules providing ready-to-use functionalities ranging from file I/O operations to network communications.

Importing of Modules

Modules are imported using the import statement. Once imported, you can access the functions, variables, and classes defined in that module using the dot notation.

Example

In this example, we demonstrate how to import and use the math module.

Python3

. . . .

Explanation:

  • import math: This line imports the math module, which includes functions for mathematical operations.
  • result = math.sqrt(25): Calls the sqrt function from the math module to calculate the square root of 25.
  • The result is printed, demonstrating how the function from the module is used.

Importing Specific Attributes from a Module

You can also choose to import specific attributes (functions, classes, or variables) from a module instead of the entire module. This is done using the from keyword.

Example

In this example, we will import and use a specific function from the math module.

Python3

. . . .

Explanation:

  • from math import sqrt: This imports only the sqrt function from the math module.
  • result = sqrt(49): Since sqrt is imported directly, it can be used without the math. prefix.
  • The result is then printed, showing how the directly imported function is used.

Using Aliases for Imports

Sometimes it is convenient or necessary to rename a module or its attributes when importing them, to avoid conflicts with existing names or simply for ease of use. This can be done using the as keyword.

Example

In this example, we import a module and a function with aliases.

Python3

. . . .

Explanation:

  • import math as m: The math module is imported with an alias m, allowing us to reference it as m instead of math.
  • from math import sqrt as square_root: The sqrt function is imported with the alias square_root.
  • result = square_root(m.pi): Demonstrates the use of both aliases in a single expression to calculate the square root of the mathematical constant pi.
  • The result is displayed, showcasing the use of aliased imports for clarity and convenience.

Understanding how to import and use modules effectively is crucial for developing scalable and maintainable Python applications. Modules enhance code reusability and help organize functionality into distinct namespaces, reducing conflicts and errors.

Python - Variable scope
Quiz
Mark as Completed

On this page

Importing of Modules

Example

Importing Specific Attributes from a Module

Example

Using Aliases for Imports

Example